var Menu=Class.create({

  FIRST_ELEM_HEIGHT:42,
  ELEM_HEIGHT:30,
  ELEM_WIDTH:160,
  initialize:function(args)
  {
    var elem = $(args['id']);
    this.menuList = args['menu_list'];
    this.minX = [];
    this.maxX = [];
    this.minY = [];
    this.maxY= [];
    this.liIndex = -1;
    for(var i=0;i<this.menuList.length;i++)
    {
      this.minX[i] = this.ELEM_WIDTH*i;
      this.maxX[i] = this.ELEM_WIDTH*(i+1);
      this.minY[i] = 0;
      if(this.menuList[i].nodes)
      {
        this.maxY[i] = (this.menuList[i].nodes.length * this.ELEM_HEIGHT)+ this.FIRST_ELEM_HEIGHT;
      }
      else
      {
        this.maxY[i] = 42;
      }
    }
    this.makeMenu({parent_elem:elem,data_list:this.menuList,level:1});
  },
  hideAll:function()
  {
    $$('.submenu').each(function(n){$(n).hide();});
    $$('.topmenulink').each(function(n){$(n).setStyle("background:url(/img/menu.gif)");});
    if (typeof document.body.style.maxHeight == "undefined") { 
      $$("select").each(function(s){ $(s).setStyle('visibility:"visible"');});
    }
  },
  checkRange:function(evt)
  {
    if(Event.pointerX(evt) < this.minX[this.liIndex] || 
      Event.pointerX(evt) > this.maxX[this.liIndex] || 
      Event.pointerY(evt) < this.minY[this.liIndex] || 
      Event.pointerY(evt) > this.maxY[this.liIndex] )
      {
        this.hideAll();
        Event.stopObserving(document,"mousemove");
      }
  },
  makeMenu:function(args)
  {
    var parentElem = args['parent_elem'];
    var dataList = args['data_list'];
    var level = args['level'];

    var ulElem = null;
    var liParams = {};
    var childParams = {};
    if(level==1)
    {
      ulElem = new Element('ul',{className:'topmenu'});
      liParams = {className:'topmenuli'};
      childParams = {className:'topmenulink'};
    }
    else
    {
      ulElem = new Element('ul',{className:'submenu'});
      childParams = {className:'submenulink'};
      ulElem.hide();
    }
    parentElem.insert(ulElem);
    for(var i=0;i<dataList.length;i++)
    {
      var rec = dataList[i];
      var liElem = new Element('li',liParams);
      ulElem.insert(liElem);
      var childElem  = null;
      if(rec.url)
      {
        childParams.href=rec.url;
        childElem = new Element('a',childParams);
      }
      else
      {
        childElem = new Element('span',childParams);
      }
      if(level == 1)
      {
        liElem.observe('mouseover',function(e,liElem,i)
        {
          this.hideAll();
          this.liIndex = i;
          if (typeof document.body.style.maxHeight == "undefined") { 
            $$("select").each(function(s){$(s).setStyle('visibility:"hidden"');});
          }
          for(var j=0;j<liElem.childNodes.length;j++)
          {
            if(liElem.childNodes[j].nodeName == 'UL')
            {
              liElem.childNodes[j].show();
            }
            else
            {
              $(liElem.childNodes[j]).setStyle("background:url(/img/menu_over.gif)");
            }
          }
          Event.observe(document, "mousemove", function(evt){
            this.checkRange(evt);
          }.bind(this));
        }.bindAsEventListener(this,liElem,i));
      }
      else
      {
        childElem.observe('mouseout',function(e)
        {
          $(e.element()).setStyle("background:#eef;color:#36b");
        });
        childElem.observe('mouseover',function(e)
        {
          $(e.element()).setStyle("background:#bbe;color:white;");
        });
      }
      childElem.insert(rec.title);
      liElem.insert(childElem);
      if(rec.nodes)
      {
        this.makeMenu({parent_elem:liElem,data_list:rec.nodes,level:level+1});
      }
    }
    return;
  }
});
